<%--
    Document   : allStops
    Created on : Jul 28, 2009, 12:58:46 PM
    Author     : david
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"
        import="com.appspot.nnaplanner.*,
        java.util.List,
        java.util.Set,
        com.google.appengine.api.datastore.Query,
        com.google.appengine.api.datastore.DatastoreServiceFactory,
        com.appspot.nnaplanner.DAL.*,
        java.util.logging.Logger"%>
<%
            Logger log = Logger.getLogger(this.getServletInfo());
            DALConnection dc = new DALConnection();
            Query query = new Query(DALConnection.class.getSimpleName());
            int total = DatastoreServiceFactory.getDatastoreService().prepare(query).countEntities();
            int start = 0 + Integer.parseInt(request.getParameter("start"));
            int limit = 0 + Integer.parseInt(request.getParameter("limit"));
            String keyword = "" + request.getParameter("keyword");
            //System.console().
            if (keyword.equals("null")) {
                keyword = "";
            }
 
            log.info("limit:" + limit + ", start: " + start + ", keyword:" + keyword);

            List<DALConnection> result = dc.getAllConnections(start, limit, keyword.toLowerCase());

            out.print("{\"total\": \"" + total + "\", \"connections\":[");
            int count = 0;
            for (DALConnection d : result) {
                if (count > 0) {
                    out.print(", ");
                }
                out.print("{");
                out.print("\"id\": \"" + d.getID() + "\", ");
                out.print("\"sid\": \"" + d.getStopID() + "\", ");
                out.print("\"sname\": \"" + d.getStopName() + "\", ");
                out.print("\"did\": \"" + d.getDestinationID() + "\", ");
                out.print("\"dname\": \"" + d.getDestinationName() + "\", ");
                if (d.getTimes() != null) {
                    out.print("\"departures\": \"" + d.getTimes().size() + "\", ");
                } else {
                    out.print("\"departures\": \"0\"");
                }
                out.print("}");
                count++;
            }
            out.print("] }");

%>
